Skip to content

refactor: improve type encapsulation in reassembly module#33

Merged
Zious11 merged 6 commits intodevelopfrom
worktree-reassembly-encapsulation
Apr 7, 2026
Merged

refactor: improve type encapsulation in reassembly module#33
Zious11 merged 6 commits intodevelopfrom
worktree-reassembly-encapsulation

Conversation

@Zious11
Copy link
Copy Markdown
Owner

@Zious11 Zious11 commented Apr 7, 2026

Summary

  • Makes FlowKey fields private with read-only accessors — prevents HashMap key mutation after insertion
  • Moves insert_segment and flush_contiguous from standalone functions to FlowDirection methods — consolidates mutation logic on the type
  • Extracts close_flow helper on TcpReassembler — deduplicates 5 identical flush-remove-notify patterns (RST, FIN, expire, evict, finalize) into one method; fixes bytes_reassembled counting inconsistency
  • Adds max_receive_window config (default 1MB, matching Suricata/Zeek/Snort) — rejects segments with offsets beyond base_offset + max_receive_window with new InsertResult::OutOfWindow variant

Closes #12

Test plan

  • All 121 existing tests pass with refactored code
  • test_out_of_window_segment_rejected — unit test: rejects beyond window, accepts at boundary, off-by-one verified
  • test_out_of_window_segment_rejected_by_engine — engine test: small window (1000 bytes), verifies stats counter and edge acceptance
  • test_finalize_bytes_reassembled_consistent — regression guard: bytes_reassembled matches total bytes delivered to handler
  • cargo fmt clean, cargo clippy clean
  • Multi-agent PR review (code, errors, tests, types) — 1 important issue found and fixed (close_flow triple lookup → single .remove())

Deferred items

Zious11 added 5 commits April 7, 2026 13:12
Prevents HashMap key mutation after insertion. Adds lower_ip(),
lower_port(), upper_ip(), upper_port() accessors. Updates dispatcher
and tests to use accessor syntax.

Part of #12
… methods

Consolidates segment mutation logic as methods on the type they
operate on. Standalone functions in segment.rs become impl
FlowDirection methods. All callers updated to method syntax.

Part of #12
Deduplicates the flush-remove-notify pattern used by RST, FIN,
expire, evict, and finalize into a single close_flow() method.
Fixes minor inconsistency: expire/evict/finalize now count
bytes_reassembled during final flush (RST/FIN already did).

Part of #12
Segments with offsets beyond base_offset + max_receive_window are
rejected with InsertResult::OutOfWindow. Default 1MB matches
Suricata/Zeek/Snort industry defaults. Counter-only, no Finding
generated (matches industry practice).

Part of #12
Replaces triple lookup (.get + .get_mut + .remove) with single
.remove() returning owned flow. Eliminates .expect() panic risk
and simplifies memory accounting. Adds off-by-one boundary test
for max_receive_window (window+1 rejected, window accepted).

Part of #12
Adds TLS to protocol analysis, features, architecture diagram, and
component table. Updates --tls flag description (no longer "coming
soon"). Removes TLS from roadmap (implemented in #30). Adds
max_receive_window to reassembly feature description.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the TCP reassembly module to strengthen type encapsulation and centralize mutation logic, while adding a configurable forward receive window to reject segments that are too far ahead of the current reassembly point.

Changes:

  • Make FlowKey fields private and add read-only accessors to prevent mutation after use as a HashMap key.
  • Move insert_segment / flush_contiguous into FlowDirection methods and add max_receive_window enforcement with a new InsertResult::OutOfWindow.
  • Deduplicate multiple “flush → remove → notify” paths into TcpReassembler::close_flow() and align bytes_reassembled counting with delivered bytes.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/reassembly/flow.rs Privatizes FlowKey fields and adds read-only accessors.
src/reassembly/segment.rs Moves segment insertion/flush into FlowDirection and adds out-of-window rejection.
src/reassembly/mod.rs Wires new APIs, adds max_receive_window config/stats, and introduces close_flow() helper.
src/dispatcher.rs Updates FlowKey field access to use new accessors.
tests/reassembly_flow_tests.rs Updates tests to use FlowKey accessors.
tests/reassembly_segment_tests.rs Updates tests for method-based segment operations; adds out-of-window unit test.
tests/reassembly_engine_tests.rs Adds engine-level tests for out-of-window behavior and bytes_reassembled consistency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/reassembly/segment.rs
@Zious11 Zious11 merged commit a6a79fd into develop Apr 7, 2026
4 checks passed
@Zious11 Zious11 deleted the worktree-reassembly-encapsulation branch April 7, 2026 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: improve type encapsulation in reassembly module

2 participants